package test;

import java.util.ArrayList;
import java.util.List;

//二叉树的所有路径：https://leetcode.cn/problems/binary-tree-paths/
public class BinartTreePaths {
	public static List<String> ret;
    public static StringBuilder _path;
    public static void dfs(TreeNode root,StringBuilder _path){
        if(root==null) return ;
        //使用变量进行回溯
        StringBuilder path=new StringBuilder(_path);
        path.append(root.val);
        //遇到叶子节点就进行收集
        if(root.left==null&&root.right==null){
            ret.add(path.toString());
            return ;
        }
        path.append("->");
        dfs(root.left,path);
        dfs(root.right,path);
    }
    public List<String> binaryTreePaths(TreeNode root) {
        _path=new StringBuilder();
        ret=new ArrayList<>();
        dfs(root,_path);
        return ret;
    }
}
